﻿using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;

namespace Ags.MonsterTools.PingTool
{
    public class LimitedObservableCollection<T> : ObservableCollection<T>
    {
        private readonly int _maxItems;

        public LimitedObservableCollection(int maxItems)
        {
            _maxItems = maxItems;
        }

        public LimitedObservableCollection()
        {
        }

        public new void Add(T item)
        {
            if (this.Count > _maxItems)
            {
                var firstEntry = this.FirstOrDefault();
                this.Remove(firstEntry);
            }

            base.Add(item);
        }

        public void RaiseCollectionChanged()
        {
            if (this.Count > 0)
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, this.Last()));
        }
    }
}
